package com.sn.campus.utils;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class GetPlatformData {

    @Autowired
    private RestTemplate restTemplate;

//    public static String url= "http://faceopenapi.ymiot.net/api";
    /**
     * 平台分配的应用Id,用于标识用户，用户将APPID 放入访问请求，以便智能设备平台识别访问者的身份
     */
    public static String appid = "pm9776621402d9a9f1";
    /**
     * 用于加密签名字符串和服务器端验证签名字符串的密钥
     */
    public static String appsecret = "21a8667dc55140739bfa42d87928e8a1";

    /**
     * 获取平台社区的设备列表
     * @return
     */
    public String getDeviceList(){
        String url = "http://faceopenapi.ymiot.net/api/Device/GetDeviceList";

        //设备编号
//        String deviceno = "0A20050820860";
        //社区编号
        String	communityno = "2005130013492251";
        String data = "{ \"pageindex\": \"1\",\"pagesize\": \"50\"}";

        String timestemp = (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
//		params.put("communityno",communityno);
        params.put("communityno", communityno);
        params.put("timestemp", timestemp);
        params.put("data", data);
        String sign = MD5.md5(untils.createLinkString(params) + "&key=" + appsecret);
        params.put("sign", sign);
        JSONObject json = JSONObject.fromObject(params);
//        String json = params.toString();
//        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(Collections.unmodifiableMap(params),headers);
//        JSONObject post = JSONObject.fromObject(request);

        ResponseEntity<String> entity = restTemplate.postForEntity(url, json, String.class);

        String body = entity.getBody();
        System.out.println(body);
        return null;
    }

    public String getCityList(){
        String url = "http://faceopenapi.ymiot.net/api/DataItem/GetCommunityByCity";

        //设备编号
//        String deviceno = "0A20050820860";
        //社区编号
//        String	communityno = "2005130013492251";
        String data = "{ \"community_cityno\": \"532502000000\"}";
//        String community_cityno = "532502000000";

        String timestemp = (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
//		params.put("communityno",communityno);
//        params.put("communityno", communityno);
        params.put("timestemp", timestemp);

//        params.put("data", data);
        String sign = MD5.md5(untils.createLinkString(params) + "&key=" + appsecret);
        params.put("sign", sign);
        JSONObject json = JSONObject.fromObject(params);
//        String json = params.toString();
//        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(Collections.unmodifiableMap(params),headers);
//        JSONObject post = JSONObject.fromObject(request);

        ResponseEntity<String> entity = restTemplate.postForEntity(url, json, String.class);

        String body = entity.getBody();
        System.out.println(body);
        return null;
    }

    public String getProvideList(){
        String url = "\thttp://faceopenapi.ymiot.net/api/DataItem/GetProvince";





        String timestemp = (new SimpleDateFormat("yyyyMMddHHmmssSSS")).format(new Date());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        Map<String, String> params = new HashMap<String, String>();
        params.put("appid", appid);
//		params.put("communityno",communityno);
//        params.put("communityno", communityno);
        params.put("timestemp", timestemp);

//        params.put("data", data);
        String sign = MD5.md5(untils.createLinkString(params) + "&key=" + appsecret);
        params.put("sign", sign);
        JSONObject json = JSONObject.fromObject(params);
//        String json = params.toString();
//        HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(Collections.unmodifiableMap(params),headers);
//        JSONObject post = JSONObject.fromObject(request);

        ResponseEntity<String> entity = restTemplate.postForEntity(url, json, String.class);

        String body = entity.getBody();
        System.out.println(body);
        return null;
    }
}
